import { Directive } from 'vue'
export const permission: Directive = {
  mounted(el: any, binding: any) {
    const { value } = binding
    const roles: any = sessionStorage.getItem('permission')?.split(",")
    if(value && value instanceof Array) {
      if (value.length > 0) {
        const permissionRoles = value
        const hasPermission = roles.some((role: any) => {
          return permissionRoles.includes(role)
        })
        if (!hasPermission) {
          el.parentNode && el.parentNode.removeChild(el)
        }
        // 当用户没有这个按钮权限时，设置隐藏这个按钮
        // if (!hasPermission) {
        //   el.style.display = 'none'
        // }
      }
    } else {
      throw new Error(`need roles! Like v-permission="['sys:menu:save']"`)
    }
  }
}